﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace GPS_Simulator
{
    public partial class comport : Form
    {
        Form1 form1;
        public System.IO.Ports.SerialPort serialPort1;
        string[] theSerialPortNames = System.IO.Ports.SerialPort.GetPortNames();
        public comport(Form1 form)
        {
            InitializeComponent();
            form1 = form;
            serialPort1 = form.serialPort1;
        }

        private void cmbPortNum_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] file_data = { cmbPortNum.Text, txtBaud.Text, txtData_Bits.Text, txtStop_Bits.Text, txtParity.Text };
            System.IO.File.WriteAllLines(form1.comport_path, file_data);
            this.Close();
        }

        private void comport_Load(object sender, EventArgs e)
        {
            string txtPath = Application.StartupPath;
            string newFileName = "comport.txt";
            txtPath = System.IO.Path.Combine(txtPath, newFileName);
            int i = 0;
            //comboBox4.Items.Clear();
            cmbPortNum.BeginUpdate();
            foreach (string port in theSerialPortNames)
            {

                cmbPortNum.Items.Add(port);
                i++;
            }
            cmbPortNum.EndUpdate();
            cmbPortNum.Text = "Com13";
            txtBaud.Text = "9600";
            txtData_Bits.Text = "8";
            txtParity.Text = "None";
            txtStop_Bits.Text = "1";
            cmbPortNum.SelectedIndex = 0;
        }

        private void txtBaud_KeyPress(object sender, KeyPressEventArgs e)
        {
            char c = e.KeyChar;
            if (c != '\b' && !((c >= 0x30 && c <= 0x39)))
            {
                e.Handled = true;
            }
        }

        private void txtBaud_Leave(object sender, EventArgs e)
        {
            Control control = (Control)sender;
            if (control.Text.Length == 0)
            {
                control.Text = "9600";
            }
        }
    }
}
